package com.zhangck.bos2_0.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangck.bos2_0.Service.CourierService;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;


/** 
* @author 上午茶
* @version 创建时间：2018年5月30日 上午9:42:00 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

	private Courier coruier = new Courier();
	
	@Autowired
	private CourierService courierService;
	
	@Override
	public Courier getModel() {
		return coruier;
	}
	
	@Action(value="CourierAction_save",results={@Result(name="success", type="redirect", location="/pages/base/courier.html")})
	public String save(){
		courierService.save(coruier);
		return  "success";
	}

	private String row;
	
	public void setRow(String row) {
		this.row = row;
	}

		//作废方法
	@Action(value="CourierAction_delete",results={@Result(name="success", type="redirect", location="/pages/base/standard.html")})
	public String delete(){
		String row = ServletActionContext.getRequest().getParameter("row");
		System.out.println(row);
		String[] split = row.split("-");
		System.out.println(split.length);
		courierService.update(split);
		return  "success";
	}
	
	//还原
	@Action(value="CourierAction_Restore",results={@Result(name="success", type="redirect", location="/pages/base/standard.html")})
	public String Restore(){
		String row = ServletActionContext.getRequest().getParameter("row");
		System.out.println(row);
		String[] split = row.split("-");
		System.out.println(split.length);
		courierService.updateRestore(split);
		return  "success";
	}
	

	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//分页
	@Action(value="CourierAction_page",results={@Result(name="success",type="json")})
	public String  CourierAction_page(){
		Pageable request = new PageRequest(page-1, rows);
		Specification<Courier> specification = new Specification<Courier>() {
		
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				//等值查询
				if (StringUtils.isNotBlank(coruier.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class),
							coruier.getCourierNum());
					list.add(p1);
				}
				//模糊查询
				if (StringUtils.isNotBlank(coruier.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), 
							"%"+coruier.getCompany()+"%");
					list.add(p2);
				}
				//等值查询
				if (StringUtils.isNotBlank(coruier.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class),
							coruier.getCompany());
					list.add(p3);
				}
				//这里root是coruier join是standard
				Join<Courier, Standard> join = root.join("standard",JoinType.LEFT);
				if (coruier.getStandard()!=null&&StringUtils.isNotBlank(coruier.getStandard().getName())) {
					Predicate p4 = cb.like(join.get("name").as(String.class), "%"+coruier.getStandard().getName()+"%");
					list.add(p4);
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		
		Page<Courier> page1 = courierService.findPageData(specification,request);

		Map<String, Object> map = new HashMap();
		map.put("total", page1.getTotalElements());//必须得用total接受
		map.put("rows", page1.getContent());
		ActionContext.getContext().getValueStack().push(map);
		
		return  "success";
	}

	@Action(value="CoueierAction_find",results={@Result(name="success",type="json")})
	public String  CoueierAction_find(){
	  List<Courier> list= courierService.findCourierbox();
	  ActionContext.getContext().getValueStack().push(list);
		return "success";
	}

}
